import java.util.Scanner;

public class D1 {
public static void main(String[] args) {
	Scanner scanner=new Scanner(System.in);
	System.out.println("请输入年份：");
	int year=scanner.nextInt();
	System.out.println("请输入月份：");
	int mouth=scanner.nextInt();
	
	if (mouth>12||mouth<=0) {
		System.out.println("输入错误");
	}else {
		int totalDays=getTotalDays(year, mouth);
		System.out.println("输入日期距1900年1月1日共"+totalDays+"天");
		
		//设置打印格式
		System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日");
		int week=totalDays%7+1;//当月1号是周几
		
		if (week==1) {// 周一输出0个\t
			week=0;
		}
		//输出1号前面的\t,当月1号是周一输出0个\t，周二输出1个\t
		for (int i = 1; i < week; i++) {
			System.out.print(" \t");
		}
		//获取当月的天数
		int days=getMouthDays(year, mouth);
		for (int i = 1; i < days; i++) {
			System.out.print("  "+i+"\t");
			if ((totalDays+i)%7==0) {//逢周日换行
				System.out.println();
			}
		}
	}
	
	
}
public static boolean isR(int year) {// 指定年份是否闰年
	if (year%4==0 && year%100!=0 || year%400==0) 
		return true;
	else 
		return false;
}
public static int getYearDays(int year) {//指定的年有多少天
	//int yearDays;
	/*if (isR(year)) 
		yearDays=366;
	else 
		yearDays=365;
	return yearDays;*/
	return isR(year)?366:365;
}
public static int getMouthDays(int year,int mouth) {//指定的年\月拥有的天数
	switch (mouth) {
	case 4:
	case 6:
	case 9:
	case 11:
		return 30;
	case 2:
		if (isR(year)) 
			return 29;
		else 
			return 28;
	default:
		return 31;
	}
}
private static int getTotalDays(int year,int mouth) {//获取1900年1月1日到指定年月之前那个月的总天数
	int totalDays=0;
	for (int i = 1900; i < year; i++) {//年
		totalDays +=getYearDays(i);
	}
    for (int j = 1; j < mouth; j++) {//月
    	totalDays +=getMouthDays(year,j);
	}
    return totalDays;
}

}
